#include "AtDevice.h"
#include "../TinyXML/tinyxml.h"
#include <QDebug>
#include "../ChineseEnglishMapping.h"
#include <QVBoxLayout>
#include <QSplitter>

AtDevice::AtDevice()
{

}

void AtDevice::InitUI(const QString &fileName, QList<QPair<QWidget *, QString> > &widgets, QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);

    AtDeviceConnectStr _AtDeviceConnectStr;
    QList<AirBlower> _AirBlowerList;
    QList<WaterValue> _WaterValueList;
    WindPress _WindPress;
    Analysis(filePath, _AtDeviceConnectStr, _AirBlowerList, _WaterValueList, _WindPress);

    QWidget* widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);
    QSplitter *splitter = new QSplitter(Qt::Vertical, widget);
    splitter->setChildrenCollapsible(false);
    layout->addWidget(splitter);
    widgets.append(qMakePair(widget,QStringLiteral("tab0")));

    TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
    splitter->addWidget(_TableWidgetForm);
    _TableWidgetForm->setGroupBoxTitle(QString("%1_0").arg(fileName.left(fileName.size()-4)));

    //获取表头
    _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_AtDeviceConnectStr.toFieldNames().split(',')));
    _TableWidgetForm->setTableWidgetHeaderType(_AtDeviceConnectStr.toFieldTypes());

    _TableWidgetForm->getTableWidget()->setRowCount(1);
    _TableWidgetForm->getTableWidget()->setItem(0, 0, new QTableWidgetItem(_AtDeviceConnectStr.name));
    _TableWidgetForm->getTableWidget()->setItem(0, 1, new QTableWidgetItem(_AtDeviceConnectStr.type));

    TableWidgetForm* _TableWidgetForm2 = new TableWidgetForm(widget);
    splitter->addWidget(_TableWidgetForm2);
    _TableWidgetForm2->setGroupBoxTitle(QString("%1_1").arg(fileName.left(fileName.size()-4)));

    //获取表头
    AirBlower _AirBlowertemp;
    _TableWidgetForm2->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_AirBlowertemp.toFieldNames().split(',')));
    _TableWidgetForm2->setTableWidgetHeaderType(_AirBlowertemp.toFieldTypes());

    for (auto _AirBlower : _AirBlowerList)
    {
        int rowCount = _TableWidgetForm2->getTableWidget()->rowCount();
        _TableWidgetForm2->getTableWidget()->setRowCount(rowCount+1);
        _TableWidgetForm2->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(_AirBlower.name));
        _TableWidgetForm2->getTableWidget()->item(rowCount,0)->setData(Qt::UserRole,_AirBlower.id);
        _TableWidgetForm2->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(QString("%1").arg(_AirBlower.rotate)));
        _TableWidgetForm2->getTableWidget()->setItem(rowCount, 2, new QTableWidgetItem(QString("%1").arg(_AirBlower.board_id)));
        _TableWidgetForm2->getTableWidget()->setItem(rowCount, 3, new QTableWidgetItem(QString("%1").arg(_AirBlower.port)));
    }

    TableWidgetForm* _TableWidgetForm3 = new TableWidgetForm(widget);
    splitter->addWidget(_TableWidgetForm3);
    _TableWidgetForm3->setGroupBoxTitle(QString("%1_2").arg(fileName.left(fileName.size()-4)));

    //获取表头
    WaterValue _WaterValue;
    _TableWidgetForm3->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_WaterValue.toFieldNames().split(',')));
    _TableWidgetForm3->setTableWidgetHeaderType(_WaterValue.toFieldTypes());

    for (auto _WaterValue : _WaterValueList)
    {
        int rowCount = _TableWidgetForm3->getTableWidget()->rowCount();
        _TableWidgetForm3->getTableWidget()->setRowCount(rowCount+1);
        _TableWidgetForm3->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(_WaterValue.name));
        _TableWidgetForm3->getTableWidget()->item(rowCount,0)->setData(Qt::UserRole,_WaterValue.id);
        _TableWidgetForm3->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(QString("%1").arg(_WaterValue.angle)));
        _TableWidgetForm3->getTableWidget()->setItem(rowCount, 2, new QTableWidgetItem(QString("%1").arg(_WaterValue.board_id)));
        _TableWidgetForm3->getTableWidget()->setItem(rowCount, 3, new QTableWidgetItem(QString("%1").arg(_WaterValue.port)));
    }

    TableWidgetForm* _TableWidgetForm4 = new TableWidgetForm(widget);
    splitter->addWidget(_TableWidgetForm4);
    _TableWidgetForm4->setGroupBoxTitle(QString("%1_3").arg(fileName.left(fileName.size()-4)));

    //获取表头
    WindPress _WindPresstemp;
    _TableWidgetForm4->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_WindPresstemp.toFieldNames().split(',')));
    _TableWidgetForm4->setTableWidgetHeaderType(_WindPresstemp.toFieldTypes());

    _TableWidgetForm4->getTableWidget()->setRowCount(1);
    _TableWidgetForm4->getTableWidget()->setItem(0, 0, new QTableWidgetItem(_WindPress.name));
    _TableWidgetForm4->getTableWidget()->item(0,0)->setData(Qt::UserRole,_WindPress.id);
    _TableWidgetForm4->getTableWidget()->setItem(0, 1, new QTableWidgetItem(QString("%1").arg(_WindPress.test)));

    m_pTableWidgetFormMap.insert(0,QList<TableWidgetForm*>() << _TableWidgetForm << _TableWidgetForm2 << _TableWidgetForm3<< _TableWidgetForm4 );
}

bool AtDevice::SaveUI(const QString &fileName, const QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    if(m_pTableWidgetFormMap.size() > 0)
    {
        AtDeviceConnectStr _AtDeviceConnectStr;
        QList<AirBlower> _AirBlowerList;
        QList<WaterValue> _WaterValueList;
        WindPress _WindPress;

        TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(0);
        if(_TableWidgetForm)
        {
            _AtDeviceConnectStr.name = _TableWidgetForm->getTableWidget()->item(0, 0)->text();
            _AtDeviceConnectStr.type = _TableWidgetForm->getTableWidget()->item(0, 1)->text();
        }

        TableWidgetForm* _TableWidgetForm2 = m_pTableWidgetFormMap.value(0).at(1);
        if(_TableWidgetForm2)
        {
            for (int k = 0;k<_TableWidgetForm2->getTableWidget()->rowCount();k++)
            {
                AirBlower _AirBlower;
                _AirBlower.id = _TableWidgetForm2->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                _AirBlower.name = _TableWidgetForm2->getTableWidget()->item(k,0)->text();
                _AirBlower.rotate = _TableWidgetForm2->getTableWidget()->item(k,1)->text().toInt();
                _AirBlower.board_id = _TableWidgetForm2->getTableWidget()->item(k,2)->text().toInt();
                _AirBlower.port = _TableWidgetForm2->getTableWidget()->item(k,3)->text().toInt();
                _AirBlowerList.append(_AirBlower);
            }
        }

        TableWidgetForm* _TableWidgetForm3 = m_pTableWidgetFormMap.value(0).at(2);
        if(_TableWidgetForm3)
        {
            for (int k = 0;k<_TableWidgetForm3->getTableWidget()->rowCount();k++)
            {
                WaterValue _WaterValue;
                _WaterValue.id = _TableWidgetForm3->getTableWidget()->item(k,0)->data(Qt::UserRole).toInt();
                _WaterValue.name = _TableWidgetForm3->getTableWidget()->item(k,0)->text();
                _WaterValue.angle = _TableWidgetForm3->getTableWidget()->item(k,1)->text().toInt();
                _WaterValue.board_id = _TableWidgetForm3->getTableWidget()->item(k,2)->text().toInt();
                _WaterValue.port = _TableWidgetForm3->getTableWidget()->item(k,3)->text().toInt();
                _WaterValueList.append(_WaterValue);
            }
        }

        TableWidgetForm* _TableWidgetForm4 = m_pTableWidgetFormMap.value(0).at(3);
        if(_TableWidgetForm4)
        {
            _WindPress.id = _TableWidgetForm4->getTableWidget()->item(0, 0)->data(Qt::UserRole).toInt();
            _WindPress.name = _TableWidgetForm4->getTableWidget()->item(0, 0)->text();
            _WindPress.test = _TableWidgetForm4->getTableWidget()->item(0, 1)->text().toInt();
        }
        SaveXml(filePath, _AtDeviceConnectStr, _AirBlowerList, _WaterValueList, _WindPress);
        return true;
    }
    return false;
}

void AtDevice::Analysis(const QString &path, AtDeviceConnectStr &_AtDeviceConnectStr, QList<AirBlower> &_AirBlowerList, QList<WaterValue> &_WaterValueList, WindPress &_WindPress)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("AtDevices");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    if(rootElement)
    {
        TiXmlElement* ConnectStrElement = rootElement->FirstChildElement("ConnectStr");
        if(ConnectStrElement != nullptr)
        {
            _AtDeviceConnectStr.name = ConnectStrElement->Attribute("name");
            _AtDeviceConnectStr.type = ConnectStrElement->Attribute("type");
        }

        for(TiXmlElement* AirBlowerElement = rootElement->FirstChildElement("AirBlower");
            AirBlowerElement != nullptr; AirBlowerElement = AirBlowerElement->NextSiblingElement("AirBlower"))
        {
            AirBlower _AirBlower;
            AirBlowerElement->QueryIntAttribute("id", &_AirBlower.id);
            _AirBlower.name = AirBlowerElement->Attribute("name");
            AirBlowerElement->QueryIntAttribute("rotate", &_AirBlower.rotate);
            AirBlowerElement->QueryIntAttribute("board_id", &_AirBlower.board_id);
            AirBlowerElement->QueryIntAttribute("port", &_AirBlower.port);
            _AirBlowerList.append(_AirBlower);
        }

        for(TiXmlElement* WaterValueElement = rootElement->FirstChildElement("WaterValue");
            WaterValueElement != nullptr; WaterValueElement = WaterValueElement->NextSiblingElement("WaterValue"))
        {
            WaterValue _WaterValue;
            WaterValueElement->QueryIntAttribute("id", &_WaterValue.id);
            _WaterValue.name = WaterValueElement->Attribute("name");
            WaterValueElement->QueryIntAttribute("angle", &_WaterValue.angle);
            WaterValueElement->QueryIntAttribute("board_id", &_WaterValue.board_id);
            WaterValueElement->QueryIntAttribute("port", &_WaterValue.port);
            _WaterValueList.append(_WaterValue);
        }

        TiXmlElement* WindPressElement = rootElement->FirstChildElement("WindPress");
        if(WindPressElement != nullptr)
        {
            WindPressElement->QueryIntAttribute("id", &_WindPress.id);
            _WindPress.name = WindPressElement->Attribute("name");
            WindPressElement->QueryIntAttribute("test", &_WindPress.test);
        }
    }
    delete xmlDocument;
}

bool AtDevice::SaveXml(const QString &path, const AtDeviceConnectStr &_AtDeviceConnectStr, const QList<AirBlower> &_AirBlowerList, const QList<WaterValue> &_WaterValueList, const WindPress &_WindPress)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("AtDevices");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
    }

    if(rootElement)
    {
        TiXmlElement* ConnectStrElement = rootElement->FirstChildElement("ConnectStr");
        if(ConnectStrElement != nullptr)
        {
            ConnectStrElement->SetAttribute("name",_AtDeviceConnectStr.name.toStdString().c_str());
            ConnectStrElement->SetAttribute("type",_AtDeviceConnectStr.type.toStdString().c_str());
        }

        int index= 0;
        for(TiXmlElement* AirBlowerElement = rootElement->FirstChildElement("AirBlower");
            AirBlowerElement != nullptr; AirBlowerElement = AirBlowerElement->NextSiblingElement("AirBlower"))
        {
            AirBlowerElement->SetAttribute("id", _AirBlowerList.at(index).id);
            AirBlowerElement->SetAttribute("name", _AirBlowerList.at(index).name.toStdString().c_str());
            AirBlowerElement->SetAttribute("rotate", _AirBlowerList.at(index).rotate);
            AirBlowerElement->SetAttribute("board_id", _AirBlowerList.at(index).board_id);
            AirBlowerElement->SetAttribute("port", _AirBlowerList.at(index).port);
            index ++;
        }

        index= 0;
        for(TiXmlElement* WaterValueElement = rootElement->FirstChildElement("WaterValue");
            WaterValueElement != nullptr; WaterValueElement = WaterValueElement->NextSiblingElement("WaterValue"))
        {
            WaterValueElement->SetAttribute("id", _WaterValueList.at(index).id);
            WaterValueElement->SetAttribute("name", _WaterValueList.at(index).name.toStdString().c_str());
            WaterValueElement->SetAttribute("angle", _WaterValueList.at(index).angle);
            WaterValueElement->SetAttribute("board_id", _WaterValueList.at(index).board_id);
            WaterValueElement->SetAttribute("port", _WaterValueList.at(index).port);
            index ++;
        }

        TiXmlElement* WindPressElement = rootElement->FirstChildElement("WindPress");
        if(WindPressElement != nullptr)
        {
            WindPressElement->SetAttribute("id", _WindPress.id);
            WindPressElement->SetAttribute("name",_WindPress.name.toStdString().c_str());
            WindPressElement->SetAttribute("test",_WindPress.test);
        }
    }
    //保存
    xmlDocument->SaveFile(path.toStdString().c_str());

    delete xmlDocument;
    return true;
}
